package com.briup.web.servlet;

import com.briup.bean.Book;
import com.briup.bean.ShopCar;
import com.briup.service.IBookService;
import com.briup.service.impl.IBookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/addShopCar")
public class addShopCarServlet extends HttpServlet {
    private IBookService bookService = new IBookServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int bookId = Integer.parseInt(req.getParameter("bookId"));
        int num = Integer.parseInt(req.getParameter("num"));

        Book book = bookService.findBookById(bookId);
        //加入购物车对象===>跳转页面
        ShopCar shopCar = (ShopCar) req.getSession().getAttribute("shopCar");
        shopCar.add(book,num);
        req.getRequestDispatcher("/shopCar.jsp").forward(req,resp);

    }
}